<template>
  <div class="header">
    <div class="title">后台管理</div>
<!--    <el-menu
      class="el-menu-demo"
      mode="horizontal"
    >
      <el-menu-item>
        <router-link style="text-decoration: none" to="/">返回首页</router-link>
      </el-menu-item>
    </el-menu>-->
    <div class="header-actions">
      <div v-if="userStore?.userInfo == null">
        <el-button type="primary" @click="goByPush('Login')" v-if="userStore?.userInfo == null">登录</el-button>
        <el-button @click="goByPush('Register')" v-if="userStore?.userInfo == null">注册</el-button>
      </div>
      <div v-else>
        <el-dropdown style="cursor: pointer; height: 60px">
          <div style="display: flex; align-items: center">
            <img style="width: 40px; height: 40px; border-radius: 50%;"
                 :src="userStore.userInfo.avatar"
                 alt="未知头像">
            <span style="margin-left: 5px;">{{ userStore.userInfo.nickname }}</span>
            <el-icon>
              <arrow-down />
            </el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goByPush('home')">返回前台</el-dropdown-item>
              <el-dropdown-item @click="goByPush('Info')">个人信息</el-dropdown-item>
              <el-dropdown-item @click="goByPush('Password')">修改密码</el-dropdown-item>
              <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRoute } from 'vue-router'
import router from '@/router/index.js'
import useUserStore from '@/store/user.js'
import { ElMessage } from 'element-plus'

const route = useRoute()
const userStore = useUserStore()


const goByPush = (name) => {
  router.push({ name: name })
}

const logout = () => {
  userStore.logout().then(() => {
    ElMessage.success('退出成功')
    router.go(0)
  })
}

onMounted(() => {
  userStore.getUserInfo().then((res) => {
    res.data = userStore.userInfo
  })
})
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background-color: #f8f9fa;
  font-size: 150%;
  font-weight: bold;

  .title {
    color: green;
    font-weight: bold;
  }
}

.el-menu-demo {
  flex: 1;
  font-size: 150%;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.el-menu--horizontal.el-menu {
  border: 0;
}
</style>
